﻿namespace TestProject.FunWithExpressions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Linq.Expressions;

    public static class ExpressionTester
    {

        public static void ExpressionTest()
        {
            Console.WriteLine(" --- ExpressionTester - ExpressionTest ---");
            var calculator = new Calculator();


            //// Test Pire Cas Complexe
            var donneeMensuel = new DonneeMensuel() { Annee = 2011, ValeurOriginal = -1, ValeurOriginal2 = 12, ValeurReference = 13, ValeurReference2 = 14, MoisId = 11 };
            var donneeMensuel2 = new DonneeMensuel() { Annee = 2012, ValeurOriginal = 21, ValeurOriginal2 = 22, ValeurReference = 23, ValeurReference2 = 24, MoisId = 22 };
            var listDonneeMensuel = new List<DonneeMensuel>() { donneeMensuel, donneeMensuel2 };
            Expression<Func<IEnumerable<DonneeMensuel>, decimal?>> expression0 = x => (((x.Sum(y => y.ValeurOriginal) / x.Sum(y => y.ValeurOriginal2)) / (x.Sum(y => y.ValeurReference) / x.Sum(y => y.ValeurReference2))) + -1) * 100;
            string result0 = calculator.Calcul<IEnumerable<DonneeMensuel>>(listDonneeMensuel, expression0, "N1");
            Console.WriteLine("The REsult : {0}", result0);

            //// DivideByZero - the result should be -1
            var data1 = new DataInfo { A = 10, B = 0, C = 1 };
            Expression<Func<DataInfo, decimal?>> expression1 = x => x.A / x.B + x.C;
            var result1 = calculator.Calcul(data1, expression1, "N0");
            Console.WriteLine("DivideByZero - the result should be -1 : {0}", result1);

            //// Negative 1 - the result should be -
            var data2 = new DataInfo { A = 10, B = 5, C = -1 };
            Expression<Func<DataInfo, decimal?>> expression2 = x => x.A / x.B + x.C;
            var result2 = calculator.Calcul(data2, expression2, "N0");
            Console.WriteLine("Negative 1 - the result should be - : {0}", result2);

            //// Negative 2 - the result should be C
            var data3 = new DataInfo { A = 10, B = 5, C = -2 };
            Expression<Func<DataInfo, decimal?>> expression3 = x => x.A / x.B + x.C;
            var result3 = calculator.Calcul(data3, expression3, "N0");
            Console.WriteLine("Negative 2 - the result should be C : {0}", result3);


            //// the result should be 3
            var data4 = new DataInfo { A = 10, B = 5, C = 1 };
            Expression<Func<DataInfo, decimal?>> expression4 = x => x.A / x.B + x.C;
            var result4 = calculator.Calcul(data4, expression4, "N0");
            Console.WriteLine("the result should be 3 : {0}", result4);


            //// DivideByZero - the result should be -1
            var data5 = new List<DataInfo> {
                                    new DataInfo {A = 10, B = 0, C = 1},
                                    new DataInfo {A = 10, B = 0, C = 1}
                        };

            Expression<Func<IEnumerable<DataInfo>, decimal?>> expression5 = x => x.Sum(y => y.A) / x.Sum(y => y.B) + x.Sum(y => y.C);
            var result5 = calculator.Calcul(data5, expression5, "N0");
            Console.WriteLine("DivideByZero - the result should be -1 : {0}", result5);


            //// the result should be 4
            var data6 = new List<DataInfo> {
                                    new DataInfo {A = 10, B = 5, C = 1},
                                    new DataInfo {A = 10, B = 5, C = 1}
                        };

            Expression<Func<IEnumerable<DataInfo>, decimal?>> expression6 = x => x.Sum(y => y.A) / x.Sum(y => y.B) + x.Sum(y => y.C);
            var result6 = calculator.Calcul(data6, expression6, "N0");
            Console.WriteLine("the result should be 4 : {0}", result6);


            //// the result should be -
            var data7 = new List<DataInfo> {
                                    new DataInfo {A = 10, B = 5, C = -1},
                                    new DataInfo {A = 10, B = 5, C = 1}
                        };

            Expression<Func<IEnumerable<DataInfo>, decimal?>> expression7 = x => x.Sum(y => y.A) / x.Sum(y => y.B) + x.Sum(y => y.C);
            var result7 = calculator.Calcul(data7, expression7, "N0");
            Console.WriteLine("the result should be - : {0}", result7);


            //// the result should be 14
            var c1 = 1;
            var c2 = 2;

            var data8 = new DataInfo { A = 10, B = 1, C = 1 };
            Expression<Func<DataInfo, decimal?>> expression8 = x => x.A / x.B + x.C + c1 + c2;
            var result8 = calculator.Calcul(data8, expression8, "N0");
            Console.WriteLine("the result should be 14 : {0}", result8);

        }
    }
}
